

//
//  UIDateInstallView.m
//  Time
//
//  Created by rzm on 16/6/20.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SmpuxTimePickerView.h"

#import "UIDatePicker+CustomDatePicker.h"

@interface SmpuxTimePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

//@property (nonatomic, copy) NSString *selectedStr;
@property (nonatomic, assign) NSInteger selectedIndex ;
//@property (nonatomic, strong) NSArray *speedArr;

@end
@implementation SmpuxTimePickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    [datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    CGRect dateFrame = datePicker.frame;
//    dateFrame.origin.y = Sampux6HScale(0);
    dateFrame.size.width =  self.width * 0.6;
    dateFrame.size.height = self.height;
    datePicker.backgroundColor = RandomColor;
    datePicker.frame = dateFrame;
//    datePicker.frame = CGRectMake(0, self.height/2 - Sampux6HScale(200)/2, self.width * 0.6, Sampux6HScale(200));
    [datePicker addTarget:self action:@selector(changeData:) forControlEvents:UIControlEventValueChanged];
    datePicker.datePickerMode = UIDatePickerModeTime;
    [datePicker setLocale:[NSLocale systemLocale]];
//    [self addSubview:datePicker];
//    self.datePicker = datePicker;
    [datePicker setValue:THWhiteAlplaColor(0.8) forKey:@"textColor"];
    [datePicker changeSpearatorLineColor];
    
    _hourPicker = [[SampuxHourPickerView alloc]initWithFrame:CGRectMake(0, 0, self.width *0.58, self.height)];
    [self addSubview:_hourPicker];
    CALayer *viewLayer = _hourPicker.layer;
    [viewLayer setFrame:CGRectMake(0, 0,_hourPicker.width , self.height)];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(_hourPicker.width/2 - 2, 0, 4, self.height)];
    [self addSubview:lab];
    lab.text = @":";
    lab.textColor = THWhiteAlplaColor(0.8);
    lab.textAlignment = NSTextAlignmentCenter;
}

- (void)createSpeedPickerView
{
    _speedPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(self.width * 0.7,0,  self.width * 0.2,self.height)];
    _speedPicker.delegate = self;
    _speedPicker.dataSource = self;
    [self addSubview:_speedPicker];
    [_speedPicker setValue:THWhiteAlplaColor(0.8) forKey:@"textColor"];
    
    UILabel *cLab = [[UILabel alloc]initWithFrame:CGRectMake(_speedPicker.right, 0 , Sampux6HScale(40), self.height)];
    cLab.text = @"℃";
    [self addSubview:cLab];
    cLab.textColor = THWhiteAlplaColor(0.6);
    CALayer *viewLayer = _speedPicker.layer;
    [viewLayer setFrame:CGRectMake(self.width * 0.7, 0,_speedPicker.width , self.height)];

}

- (void)setTempArr:(NSArray *)TempArr
{
    _TempArr = TempArr;
    [self createSpeedPickerView];

//    if (_TempArr.count != 0) {
//        [self createSpeedPickerView];
//    }
}

- (void)changeData:(UIDatePicker *)datePicker
{
    self.dateStr = [NSString dateTextWithDate:datePicker.date];

    if (self.installDelegate && [self.installDelegate respondsToSelector:@selector(changeDate:)]) {
        [self.installDelegate changeDate:self.dateStr];
    }
}

#pragma mark UIPickerViewDetaSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.TempArr.count *50;
}

#pragma mark UIPickerViewDelegate
/**
 *  pickerView每行的视图
 */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.minimumScaleFactor = 12.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:20]];
        pickerLabel.textColor = THWhiteAlplaColor(0.8);
    }

     ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = THWhiteAlplaColor(0.8);
     ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = THWhiteAlplaColor(0.8);
    // Fill the label text here
    pickerLabel.text = [self.TempArr objectAtIndex:(row%[self.TempArr count])];
    return pickerLabel;
}

/**
 *  轮子显示内容
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.TempArr objectAtIndex:(row%[self.TempArr count])];
}

/**
 *  轮子的移动
// */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSUInteger max = 0;
    NSUInteger base10 = 0;
    if(component == 0)
    {
        max = [_TempArr count]*50;
        base10 = (max/2)-(max/2)%[_TempArr count];
        [pickerView selectRow:[pickerView selectedRowInComponent:component]%[self.TempArr count]+base10 inComponent:component animated:false];
    }
    self.selectedIndex = [pickerView selectedRowInComponent:0]%[self.TempArr count];
    self.speedStr = self.TempArr[self.selectedIndex];
    
    if (self.installDelegate && [self.installDelegate respondsToSelector:@selector(changeSpeed:)]) {
        [self.installDelegate changeSpeed:self.speedStr];
    }
}

/**
 *  pickerView的行高
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return self.height/3;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    _selectedIndex = selectedIndex;
    
}
- (void)setDateStr:(NSString *)dateStr
{
    _dateStr = dateStr;
}

- (void)setSpeedStr:(NSString *)speedStr
{
    _speedStr = speedStr;
}

- (void)setPickerInitialRow:(NSInteger)pickerInitialRow
{
    _pickerInitialRow = pickerInitialRow + (_TempArr.count)*25;
    [_speedPicker selectRow:_pickerInitialRow inComponent:0 animated:YES];

}
@end
